package com.yxq.mdt.app.modules.bottomsheet;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.yxq.mdt.app.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @author L
 * @time 2019-10-30
 * @describe 选择性取向
 */
public class PersonalSexualSheetDialogFragment extends BottomSheetDialogFragment {
    Unbinder unbinder;
    @BindView(R.id.dialog_tv_save)
    TextView dialogTvSave;
    @BindView(R.id.dialog_check_box_man)
    CheckBox dialogCheckBoxMan;
    @BindView(R.id.dialog_check_box_women)
    CheckBox dialogCheckBoxWomen;
    @BindView(R.id.dialog_check_box_all)
    CheckBox dialogCheckBoxAll;
    @BindView(R.id.dialog_check_box_no)
    CheckBox dialogCheckBoxNo;
    @BindView(R.id.dialog_check_box_confidentiality)
    CheckBox dialogCheckBoxConfidentiality;
    String[] data = new String[]{"爱好男", "爱好女", "双性恋", "无性恋"};
    String sexual;
    int sexualShow;

    public PersonalSexualSheetDialogFragment(String sexual, int sexualShow) {
        this.sexual = sexual;
        this.sexualShow = sexualShow;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置背景透明，才能显示出layout中诸如圆角的布局，否则会有白色底（框）
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme);

    }

    private int selectPosition = -1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.personal_sexual_bottom_sheet_fragment_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        initViews();
        return view;
    }

    private void initViews() {
        dialogCheckBoxConfidentiality.setChecked(sexualShow == 1);
        if (sexual != null) {
            switch (sexual) {
                case "爱好男":
                    dialogCheckBoxMan.setChecked(true);
                    break;
                case "爱好女":
                    dialogCheckBoxWomen.setChecked(true);
                    break;
                case "双性恋":
                    dialogCheckBoxAll.setChecked(true);
                    break;
                case "无性恋":
                    dialogCheckBoxNo.setChecked(true);
                    break;
            }
        }
        dialogCheckBoxMan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                selectPosition = 0;
                if (!b) {
                    return;
                }
                dialogCheckBoxWomen.setChecked(!b);
                dialogCheckBoxAll.setChecked(!b);
                dialogCheckBoxNo.setChecked(!b);
            }
        });
        dialogCheckBoxWomen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                selectPosition = 1;
                if (!b) {
                    return;
                }
                dialogCheckBoxMan.setChecked(!b);
                dialogCheckBoxAll.setChecked(!b);
                dialogCheckBoxNo.setChecked(!b);
            }
        });
        dialogCheckBoxAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                selectPosition = 2;
                if (!b) {
                    return;
                }
                dialogCheckBoxMan.setChecked(!b);
                dialogCheckBoxWomen.setChecked(!b);
                dialogCheckBoxNo.setChecked(!b);
            }
        });
        dialogCheckBoxNo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                selectPosition = 3;
                if (!b) {
                    return;
                }
                dialogCheckBoxMan.setChecked(!b);
                dialogCheckBoxWomen.setChecked(!b);
                dialogCheckBoxAll.setChecked(!b);
            }
        });
    }

    PersonalSexualSheetDialogFragment.PersonalSexualSheetDialogFragmentInterface personalSexualSheetDialogFragmentInterface;

    public void setPersonalSexualSheetDialogFragmentInterface(PersonalSexualSheetDialogFragmentInterface personalSexualSheetDialogFragmentInterface) {
        this.personalSexualSheetDialogFragmentInterface = personalSexualSheetDialogFragmentInterface;
    }

    public interface PersonalSexualSheetDialogFragmentInterface {
        void checked(int index, String data, boolean confidentiality);
    }

    @OnClick(R.id.dialog_tv_save)
    public void onViewClicked() {
        dismiss();
        if (personalSexualSheetDialogFragmentInterface != null) {
            personalSexualSheetDialogFragmentInterface.checked(selectPosition, selectPosition != -1 ? data[selectPosition] : "", dialogCheckBoxConfidentiality.isChecked());
        }
    }
}
